//
//  LoginVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/3.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "LoginVC.h"
#import "TabbarController.h"
#import "RegisterVC.h"
@interface LoginVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *pass;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet UIButton *eyeBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeTextf;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation LoginVC
- (IBAction)codeAction:(UIButton *)sender {
    if (_phone.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
        return;
    }
    self.codeBtn.userInteractionEnabled = false;
    __block int timeout= 59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.codeBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.codeBtn setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.codeBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/user/loginCode?mobilePhone=%@",_phone.text];
    [NetRequestTool post:url
                  params:@{}
                 success:^(id  _Nonnull json) {
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];
                     }else{
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                     }
                 } failure:^(NSError * _Nonnull error) {
                 }];
}

- (IBAction)passAction:(UITextField *)sender {
}

- (IBAction)phoneAction:(UITextField *)sender {
}
- (IBAction)loginAction:(UIButton *)sender {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[NSString stringWithFormat:@"86%@",_phone.text] forKey:@"mobilePhone"];
    [params setObject:_codeTextf.text forKey:@"validateCode"];
    [params setObject:_pass.text forKey:@"userPwd"];
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/user/loginDo"];
    [NetRequestTool post:url
                  params:params
                 success:^(id  _Nonnull json) {
                     
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         
//                         [SVProgressHUD showSuccessWithStatus:@"注册成功"];
//                         [self.navigationController popViewControllerAnimated:YES];
                         
                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
    
    UIWindow *win = [self mainWindow];
    TabbarController *root = [[TabbarController alloc] init];
    win.rootViewController = root;
    
}

- (IBAction)registerAction:(UIButton*)sender {
    
    RegisterVC *vc = [[RegisterVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)eyeBtnAction:(UIButton*)sender {
    sender.selected = !sender.selected;
}

- (void)_initView{
    _phone.delegate = self;
    _phone.clearButtonMode = UITextFieldViewModeAlways;
    _pass.delegate = self;
    _pass.returnKeyType = UIReturnKeyDone;
    _loginBtn.layer.cornerRadius = 25.f;
    _registBtn.layer.cornerRadius = 25.f;
    _registBtn.layer.borderWidth = 1;
    _registBtn.layer.borderColor = [UIColor colorFromHexRGB:@"007AFF"].CGColor;
    [_eyeBtn setBackgroundImage:[UIImage imageNamed:@"yan"] forState:UIControlStateNormal];
    [_eyeBtn setBackgroundImage:[UIImage imageNamed:@"yankai"] forState:UIControlStateSelected];
    [_eyeBtn setSelected:false];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = true;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initView];
}


-(UIWindow*)mainWindow{
    id appDelegate = [UIApplication sharedApplication].delegate;
    if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
        return [appDelegate window];
    }
    
    
    NSArray *windows = [UIApplication sharedApplication].windows;
    if ([windows count] == 1) {
        return [windows firstObject];
    } else {
        for (UIWindow *window in windows) {
            if (window.windowLevel == UIWindowLevelNormal) {
                return window;
            }
        }
    }
    
    return nil;
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
    
    [textField resignFirstResponder];
    
    return YES;
    
}
@end
